//
//  LikeViewController.m
//  Movie
//
//  Created by cjy on 16/4/1.
//  Copyright © 2016年 HY. All rights reserved.
//

#import "LikeViewController.h"
#import <UIImageView+WebCache.h>
#import "PlayerViewController.h"
#import "DataBaseHandle.h"
#import "MovieModel.h"
#import "LikeTableViewCell.h"

@interface LikeViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic,retain) UITableView *tableView;
@property (nonatomic,retain)UIImageView *imageView;



@end

@implementation LikeViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.array = [[DataBaseHandle shareDataBaseHandle] select].mutableCopy;
    [self.tableView reloadData];
    if (self.array.count == 0) {
        
        self.imageView.hidden = NO;
        self.tableView.hidden = YES;
        
    } else {
        
        self.imageView.hidden = YES;
        self.tableView.hidden = NO;
        
    }
    
    
    [self.tableView reloadData];
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithWhite:0.118 alpha:1.000];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self createCell];
    

}

- (void)createCell {

    
    self.imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"sctp"]];
    
    [self.view addSubview:self.imageView];
    
    self.imageView.userInteractionEnabled = YES;
    self.imageView.frame = self.view.frame;
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[LikeTableViewCell class] forCellReuseIdentifier:@"pool2"];
    self.tableView.backgroundColor = [UIColor colorWithWhite:0.118 alpha:1.000];
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return  self.array.count;
    
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LikeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
    
    _model = [self.array objectAtIndex:indexPath.row];
    
    
    [cell passModel:_model];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
 
    return 200;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
  
    PlayerViewController *play = [[PlayerViewController alloc]init];

    play.UrlString = _model.playUrl;
    play.receiveModel = _model;

//    MovieModel *model = [self.array objectAtIndex:indexPath.row];
    
    [self showDetailViewController:play sender:nil];
    
//    [url release];
    
}



#pragma mark - 编辑删除
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    
    [super setEditing:editing animated:animated];
    
    [self.tableView setEditing:editing animated:animated];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
    
    
    
}



- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (UITableViewCellEditingStyleDelete == editingStyle) {
        
        
        [self.tableView reloadData];
        MovieModel *model = [self.array objectAtIndex:indexPath.row];
        //从数据源删除
        [self.array removeObjectAtIndex:indexPath.row];
        //从 tableView 删除
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [[DataBaseHandle shareDataBaseHandle]deleteWithModel:model];
        
        //        self.tableView.backgroundColor = [UIColor clearColor];
        
        
        if (self.array.count == 0) {
            
            self.imageView.hidden = NO;
            self.tableView.hidden = YES;
            
        } else {
            
            self.imageView.hidden = YES;
            
            self.tableView.hidden = NO;
            
        }
        
        
        
        
        
    }
    
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
